package toanstt.gameplay;

import toanstt.com.T;
import toanstt.com.Vec2;
import toanstt.sprites.Sprite;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;

public class SelectedCell 
{
	Sprite sprites[];
	int[] x;
	int[] y;
	int[] bouns; 
	int currentplus;
	int currentsub;
	int label;
	int n;
	static public int n2=0;
	public static int current_bouns;
	public static int current_bouns2;
	//for
	int temp_x,temp_y;
	int dem;
	static public int index_done_draw =0;
	public SelectedCell()
	{
		sprites = new Sprite[121];
		for(int i =0;i<121;i++)
			sprites[i] = new Sprite(T.SPRITES_CELL_SPRITES, 0, false, false);
		n=0;
		current_bouns =0;
		x = new int[T.LENG_ARRAY];
		y = new int[T.LENG_ARRAY];
		bouns = new int[T.LENG_ARRAY];
		currentplus= 10;
		currentsub = 20;
	}
	public void NewGame()
	{
		n=n2=index_done_draw=0;
	}
	public void Reset()
	{
		n=0;
		current_bouns =0;
		currentplus= 10;
		currentsub = 20;
	}
	public void Add(int x1,int y1)
	{
		if(currentplus>10000)
			currentplus = 10000;
		if(currentsub>10000)
			currentsub = 10000;
		
		if(n==0)
		{
			x[n] = x1;
			y[n] = y1;
			bouns[n] = currentplus;
			current_bouns+= currentplus;
			currentplus+= 10;
			//check for unknown bug - 
			if(x1 <0 || x1 >= T.COLS)
				if(y1 <0 || y1 >= T.ROWS)
					return;
			
			if(CellTable.cell_index[x1][y1] <= -1 )
				return;
			if(CellTable.cell[CellTable.cell_index[x1][y1]] == null)
				return;
			if(CellTable.cell[CellTable.cell_index[x1][y1]].lable >10)
				return;
			
			
			sprites[n].SetPos(T.GAMESCREEN_MIN_X + x1*T.CELL_WIDTH + T.CELL_QUAD, T.GAMESCREEN_MIN_Y + y1*T.CELL_HEIGHT + T.CELL_QUAD);
			sprites[n].ReNew();
			sprites[n].isdisable = true;
			bouns[n]*=CellTable.cell[CellTable.cell_index[x1][y1]].lucky;
			label = CellTable.cell[CellTable.cell_index[x1][y1]].lable;
					
			n++;
			T.sound.Play(T.sound.ADD);
		}
		else
		{
			for(int i =0;i<n-1;i++)
			{
				if(x[i] == x1 && y[i] == y1)
				{
					n=i+1;
					return;
				}
			}
			x[n] = x1;
			y[n] = y1;
			
			//check for unknown bug - 
			if(x1 <0 || x1 >= T.COLS)
				if(y1 <0 || y1 >= T.ROWS)
					return;
			
			if(CellTable.cell_index[x1][y1] <= -1 )
				return;
			if(CellTable.cell[CellTable.cell_index[x1][y1]] == null)
				return;
			
			if(!(x1 == x[n-1] && y1 == y[n-1]+1 || x1 == x[n-1] && y1 == y[n-1]-1 || y1 == y[n-1] && x1 == x[n-1]+1 || y1 == y[n-1] && x1 == x[n-1]-1 ))
				return;
			if(CellTable.cell[CellTable.cell_index[x1][y1]].lable >10)
				return;
			if(CellTable.cell[CellTable.cell_index[x1][y1]].lable == label )
			{
				bouns[n] = currentplus;
				bouns[n]*=CellTable.cell[CellTable.cell_index[x1][y1]].lucky;
				current_bouns+= currentplus;
				currentplus+= 10;
			}
			else
			{
				bouns[n] = -currentsub;
				current_bouns -= currentsub;
				currentsub *= 2;
			}
			sprites[n].SetPos(T.GAMESCREEN_MIN_X + x1*T.CELL_WIDTH + T.CELL_QUAD, T.GAMESCREEN_MIN_Y + y1*T.CELL_HEIGHT + T.CELL_QUAD);
			sprites[n].ReNew();
			sprites[n].isdisable = true;
			n++;
			T.sound.Play(T.sound.ADD);
		}
	}
	public void MouseEent(MotionEvent mo)
	{
		
		int action = mo.getAction();
		int actionCode = action & MotionEvent.ACTION_MASK;
		Vec2 temp =  CellTable.Getcellwithcoor((int)mo.getX(),(int)mo.getY());
		switch(actionCode)
		{
		case MotionEvent.ACTION_DOWN:
			MouseDown(temp.x, temp.y);
	    	break;
		//case MotionEvent.ACTION_UP:
			//MouseUp(temp.x, temp.y);
			//break;
		
		case MotionEvent.ACTION_MOVE:
			MouseMove(temp.x, temp.y);
			break;

		}
	}
	public void MouseDown(int x,int y)
	{
		Add(x,y);
	}
	public void MouseUp(int x,int y)
	{
		//Reset();
		DoScore();
	}
	public void MouseMove(int x1,int y1)
	{
		if(n==0) 
			return;
		if(x1 == x[n-1] && y1== y[n-1])
			return;
		//if(n>=2)
		//if(x1 == x[n-2] && y1== y[n-2])
		//{
			//n--;
			//return;	
		//}
		Add(x1,y1);
		
	}
	public void Draw(Canvas canvas)
	{
		for(int i =0;i<n;i++)
		{
			temp_x = T.GAMESCREEN_MIN_X +  x[i]*T.CELL_WIDTH;
			temp_y = T.GAMESCREEN_MIN_Y +  y[i]*T.CELL_HEIGHT;
			//T.mat = new Matrix();
			T.mat.reset();
			T.mat.postScale(T.CELL_WIDTH*1.0f/60, T.CELL_WIDTH*1.0f/60);
			T.mat.postTranslate(temp_x, temp_y);
			if(bouns[i]>0)
			{
				T.paint.setColor(Color.BLUE);
				
				canvas.drawBitmap(T.bitmap_pos, T.mat, null);
				canvas.drawText("+"+bouns[i], temp_x, temp_y, T.paint);
			}
			else
			{
				T.paint.setColor(Color.RED);
				canvas.drawBitmap(T.bitmap_neg, T.mat, null);
				canvas.drawText(""+bouns[i], temp_x, temp_y, T.paint);
			}
		}
		for(int i =0;i<n-1;i++)
			DrawLinKinElectric(i,canvas);
		
		for(int i =0;i<index_done_draw;i++)
			sprites[i].Draw(canvas,(int)(T.CELL_HEIGHT*1.5f),true );
		if(index_done_draw < n2)
		{
			current_bouns2+= bouns[index_done_draw];
			GameManagement.comment.DoComment(current_bouns2,T.GAMESCREEN_HALF_X,T.GAMESCREEN_HALF_Y);
			GameManagement.celltable.DeleteCell(x[index_done_draw], y[index_done_draw], 0);
			T.sound.Play(T.sound.PERFECT);
			index_done_draw ++;
			if(index_done_draw == n2)
			{
				T.PLAY_SCORE += current_bouns2;
				GameManagement.timeline.AddTime(current_bouns2);
			}
			
		}
	}
	public void DrawLinKinElectric(int index,Canvas canvas)
	{
		
		//T.mat = new Matrix();
		T.mat.reset();
		T.mat.postScale(T.CELL_WIDTH*1.0f/60, T.CELL_WIDTH*1.0f/60);
		if(y[index] == y[index+1])
		{
		 if(x[index] == x[index+1]-1)
		 {
			 T.mat.postTranslate(T.GAMESCREEN_MIN_X +  x[index]*T.CELL_WIDTH, T.GAMESCREEN_MIN_Y +  y[index]*T.CELL_HEIGHT);
			 canvas.drawBitmap(T.bitmap_linkin[T.random.nextInt(3)], T.mat, null);
			 return;
		 }
		 else
		 {
			 T.mat.postTranslate(T.GAMESCREEN_MIN_X +  x[index+1]*T.CELL_WIDTH, T.GAMESCREEN_MIN_Y +  y[index+1]*T.CELL_HEIGHT);
			 canvas.drawBitmap(T.bitmap_linkin[T.random.nextInt(3)], T.mat, null);
			 return;
		 }
		}
		else
		{
			if(y[index] == y[index+1]-1)
			{
				T.mat.postRotate(90,T.CELL_WIDTH/2,T.CELL_HEIGHT/2);
				T.mat.postTranslate(T.GAMESCREEN_MIN_X +  x[index]*T.CELL_WIDTH, T.GAMESCREEN_MIN_Y +  y[index]*T.CELL_HEIGHT);
				
				canvas.drawBitmap(T.bitmap_linkin[T.random.nextInt(3)], T.mat, null);
				return;
			}
			else
			{
				T.mat.postRotate(90,T.CELL_WIDTH/2,T.CELL_HEIGHT/2);
				T.mat.postTranslate(T.GAMESCREEN_MIN_X +  x[index+1]*T.CELL_WIDTH, T.GAMESCREEN_MIN_Y +  y[index+1]*T.CELL_HEIGHT);
				
				canvas.drawBitmap(T.bitmap_linkin[T.random.nextInt(3)], T.mat, null);
				return;
			}
		}
	}
	public void DoScore()
	{
		
		if(n<3)
		{
			Reset();
			return;
		}
		current_bouns2 =0;
		index_done_draw =0;
		//GameManagement.comment.DoComment(current_bouns,T.GAMESCREEN_HALF_X,T.GAMESCREEN_HALF_Y);
		
//		for(int i=0;i<n;i++)
//		{
//			GameManagement.celltable.DeleteCell(x[i], y[i], 0);
//		}
		n2 = n;
		for(int i =0;i<n2;i++)
			sprites[i].isdisable = false;
		Reset();
	}
	
}
